package cn.sptpc.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {
    private static DruidDataSource dataSource = null;
    private static DBUtil dbUtils = null;
    private static Connection conn = null;

    private DBUtil(){
        Properties props = new Properties();
        try {
            props.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(props);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DBUtil getDbUtil(){
        if (dbUtils == null){
            dbUtils = new DBUtil();
        }
        return dbUtils;
    }

    public Connection getConnection(){
        if (conn == null){
            try {
                conn = dataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return conn;
    }
}
